package com.nov.multidatasource.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.nov.multidatasource.dao.secondary.V6FileInfoMapper;
import com.nov.multidatasource.emuns.Cus7Enum;
import com.nov.multidatasource.entity.primary.V3CurrentTask;
import com.nov.multidatasource.entity.primary.V3DepartDO;
import com.nov.multidatasource.entity.secondary.V6FileInfo;
import com.nov.multidatasource.entity.secondary.V6TaskInfoEntity;
import com.nov.multidatasource.holder.ThreadLocalHolder;
import com.nov.multidatasource.service.CMFileInfoService;
import com.nov.multidatasource.utils.SnowFlakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CMFileInfoServiceImpl implements CMFileInfoService {

    @Resource
    private V6FileInfoMapper v6FileInfoMapper;

    @Override
    public V6FileInfo selectV6FileInfoByFileId(String fileId) {
        return v6FileInfoMapper.selectV6FileByFileId(fileId);
    }

    @Override
    public List<V6FileInfo> selectV6FileByBusNo(String busNo) {
        return v6FileInfoMapper.selectV6FileByBusNo(busNo);
    }

    @Override
    public int insertV6File(List<V6FileInfo> list) {
        return v6FileInfoMapper.insert(list);
    }

    @Override
    public Integer mappingAndInsertV6TaskInfo(List<V3CurrentTask> currentTaskList) {
        if (currentTaskList == null || currentTaskList.isEmpty()) {
            log.warn("没有关联的单据");
            return 0;
        }
        Map<String, List<V3DepartDO>> stringListMap = ThreadLocalHolder.departCodeMap.get();
        List<V6TaskInfoEntity> taskInfoEntities = currentTaskList.stream().map(item -> {
            V6TaskInfoEntity taskInfo = new V6TaskInfoEntity();
            taskInfo.setBusiSerialNo(item.getBusi_serial_no());
            taskInfo.setId(SnowFlakeUtil.generateId());
            taskInfo.setCreateUser(item.getUser_name());
            taskInfo.setCreateTime(item.getBilldate());
            taskInfo.setBillNo(item.getBillnum());
            taskInfo.setBillOrgCode(item.getOrgCode());
            taskInfo.setTaskTypeName(item.getTasktypename());
            taskInfo.setTaskStateName(item.getTaskStateName());
            taskInfo.setScanTypeName(item.getScanTypeName());
            taskInfo.setScanType(item.getScanType());
            taskInfo.setUserNum(item.getUser_num());
            /* 经验啊 , 摸索出来，必须要有这四个字段**/
            taskInfo.setBillType(item.getBilltype());
            taskInfo.setPkTradeType(item.getPk_billtype());
            taskInfo.setBillBelongUserNo(item.getUser_name());
            try {
                //找组织
                List<V3DepartDO> orgCodes = stringListMap.get(item.getOrgCode());
                if (CollectionUtil.isNotEmpty(orgCodes) && StrUtil.isNotBlank(item.getOrgCode())) {
                    taskInfo.setCode(orgCodes.get(0).getCODE());
                }
            } catch (Exception e) {
                log.error("获取组织信息异常", e);
            }
            taskInfo.setTaskState("3");
            taskInfo.setOperateSuggest("7");
            taskInfo.setBillTypeName(item.getBilltypeName());
            return taskInfo;
        }).collect(Collectors.toList());
        int loopCount = 0;
        for (int i = 0; i < taskInfoEntities.size(); i += 100) {
            int endIndex = Math.min(i + 100, taskInfoEntities.size());
            List<V6TaskInfoEntity> sub = CollUtil.sub(taskInfoEntities, i, endIndex);
            List<String> collect = sub.stream().map(V6TaskInfoEntity::getBusiSerialNo).filter(StrUtil::isNotBlank).collect(Collectors.toList());
            try {
                // 先删除后提交
                v6FileInfoMapper.deleteV6TaskInBusNos(collect);
                v6FileInfoMapper.batchInsertV6Task(sub);
                loopCount = loopCount + sub.size();
            } catch (Exception e) {
                ThreadLocalHolder.errorV6TaskInfoEntityList.get().addAll(sub);
                log.error("插入6.0单据信息异常", e);
            }
        }
        return loopCount;
    }

    @Override
    public void deleteV6FileInFileIds(List<String> fileIds) {
        v6FileInfoMapper.deleteV6FileInFileIds(fileIds);
    }


    @Override
    public List<V6FileInfo> selectV6FilePage(Integer page, Integer size, String startTime, String endTime) {
        return v6FileInfoMapper.selectV6FilePage(page, size, startTime, endTime);
    }
}
